package ejb.manager;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import ejb.dao.PositionDAO;
import ejb.dao.UserDAO;
import ejb.entity.City;
import ejb.entity.DBImage;
import ejb.entity.Forecast;
import ejb.entity.Position;
import ejb.entity.User;

@Stateless
public class DefaultPDFManager implements PDFManager{
	@EJB
	private CityManager cityManager;	
	@EJB
	private DBImageManager imageManager;
	@EJB
	private UserDAO userDAO;
	@EJB
	private PositionDAO positionDAO;
	@EJB
	private ForecastManager forecastManager;
	
	private ResourceBundle msg;	
	private Document doc;
	private	BaseFont bf;
	private Font titleFont, headerFont, contentFont;
	private ByteArrayOutputStream pdfDocument;
	
	public DefaultPDFManager(){
		this.msg = ResourceBundle.getBundle("Bundle");
		this.pdfDocument = new ByteArrayOutputStream();
	   	this.doc = new Document(PageSize.A4);

		try {
			this.bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
			this.titleFont = new Font(bf, 16, Font.BOLD);
			this.headerFont = new Font(bf, 12);
			this.contentFont = new Font(bf, 10);	
		} catch (Exception e) {
			this.titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
			this.headerFont = new Font(Font.FontFamily.TIMES_ROMAN, 12);
			this.contentFont = new Font(Font.FontFamily.TIMES_ROMAN, 10);
		}
	}
	
	public void createForecastPdf(){
		List<City> cities = cityManager.getAllCities();

	    try {
			PdfWriter.getInstance(doc, pdfDocument);

		    doc.open();
		    
		    Paragraph title = new Paragraph(msg.getString("forecast"), titleFont);
		    title.setAlignment(Element.ALIGN_CENTER);
		    doc.add(title);
		    
		    for(City city : cities){
		    	doc.add(new Paragraph(city.getName(), headerFont));
		    	PdfPTable table = new PdfPTable(4);			
		    	
		    	List<Forecast> forecasts = city.getForecasts();
		    	for(Forecast f : forecasts){
					PdfPCell cell = new PdfPCell(new Phrase(city.getName(), headerFont));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell.setBorder(Rectangle.NO_BORDER); 
					String date = new DefaultDateConverter().numberToDate(f.getPlusday());
		    		Paragraph forecast = new Paragraph(msg.getString("day") + " " + date, contentFont);
		    		String name = Integer.toString(f.getConditions());
		    		DBImage conditionImage = imageManager.getImage(name);
		    		Image condition = Image.getInstance(conditionImage.getImg());
			    	condition.scaleToFit(32, 32);
			    	//PdfPCell imgcell = new PdfPCell(condition, false);
			    	//imgcell.setImage(condition);
		    		Paragraph temp = new Paragraph(msg.getString("temp") + f.getTemperature() + msg.getString("celsius"), contentFont);
		    		
		    		cell.addElement(forecast);
		    		cell.addElement(condition);
		    		cell.addElement(temp);
		    		table.addCell(cell);
		    	}
				
				doc.add(table);
				doc.add(Chunk.NEWLINE);
		    }
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    doc.close();
	    
	    //to the database
	    if(imageManager.getImage("forecast") != null) imageManager.delete("forecast");
	    byte [] bytePdf = pdfDocument.toByteArray();
	    imageManager.save("forecast", bytePdf, "application/pdf");
	}
	
	public byte[] createUserForecastPDF(String username){
		System.out.println("PDF progrnoz pozycji user'a");
		User user = userDAO.getUserByLogin(username);
		
		List<Position> userPositions = positionDAO.findByUser(user);
	    try {
			PdfWriter.getInstance(doc, pdfDocument);
		    doc.open();
		    
		    Paragraph title = new Paragraph(msg.getString("forecast"), titleFont);
		    title.setAlignment(Element.ALIGN_CENTER);
		    doc.add(title);
		    doc.add(Chunk.NEWLINE);	
			for(Position position : userPositions){
		    	doc.add(new Paragraph(position.getName(), headerFont));
		    	PdfPTable table = new PdfPTable(4);	
		    	
				for(int i=1; i<5; i++){
					City city = forecastManager.getPositionForecast(position, i);	
					PdfPCell cell = new PdfPCell(new Phrase(city.getName(), headerFont));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell.setBorder(Rectangle.NO_BORDER); 
					String date = new DefaultDateConverter().numberToDate(i);
			    	Paragraph forecast = new Paragraph(msg.getString("day") + " " + date, contentFont);
			    	String name = Integer.toString(city.getConditions());
			    	DBImage conditionImage = imageManager.getImage(name);
			    	Image condition = Image.getInstance(conditionImage.getImg());
				    condition.scaleToFit(32, 32);
			    	Paragraph temp = new Paragraph(msg.getString("temp") + (int)city.getTemperature() + msg.getString("celsius"), contentFont);
			    		
			    	cell.addElement(forecast);
			    	cell.addElement(condition);
			    	cell.addElement(temp);
			    	table.addCell(cell);
				}
				doc.add(table);
				doc.add(Chunk.NEWLINE);				
			}	
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    doc.close();
	    
	    byte [] bytePdf = pdfDocument.toByteArray();
	    return bytePdf;
	}
}
